
//
//  NoteDraftManager.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/20.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "NoteDraftManager.h"
#import "NoteDraftMode.h"
#import "ImageMode.h"

@interface NoteDraftManager () {

}
@property (nonatomic,strong)NoteDraftMode *draftMode;

@end

@implementation NoteDraftManager

+ (NoteDraftManager *)sharedInstance
{
    static dispatch_once_t onceBasicToken;
    
    static NoteDraftManager *manager = nil;
    
    dispatch_once(&onceBasicToken, ^{
        manager = [[NoteDraftManager alloc] init];
    });
    return manager;
}

- (void)saveDraftMode:(NSString *)price noteTitle:(NSString *)noteTitle noteContent:(NSString *)content noteCateory:(NoteCateoryMode *)nocat disImgs:(NSMutableArray *)disImgs uplImgs:(NSMutableArray *)uplImgs {
    [self cleanDraftMode];
    //图片写入本地
   NSMutableArray *diss =  [self writeDisImgsToFile:disImgs];
   NSMutableArray *upls = [self writeUplImgsToFile:uplImgs];
    self.draftMode = [NoteDraftMode saveDraftMode:price noteTitle:noteTitle noteContent:content noteCateory:nocat disImgs:diss uplImgs:upls];
}

- (NSMutableArray *)writeUplImgsToFile:(NSMutableArray *)uplImgs {
    NSMutableArray *uplArrs = [NSMutableArray array];
    NSString *uplImgsDic = [FilePathMode getNoteDraftUploadImgFilePath];
    for (NSInteger i = 0; i < uplImgs.count; i++) {
        NSString *uplImpath = [uplImgsDic stringByAppendingPathComponent: [NSString stringWithFormat:@"draftUpl_%02ld",(long)i]];
        UIImage *image = uplImgs[i];
        NSData *imgData = UIImagePNGRepresentation(image);
        [imgData writeToFile:uplImpath atomically:YES];
        [uplArrs addObject:uplImpath];
    }
    return uplArrs;
}

- (NSMutableArray *)writeDisImgsToFile:(NSMutableArray *)disImgs {
    NSMutableArray *disArrs = [NSMutableArray array];

    NSString *disImgDic = [FilePathMode getNoteDraftDisplayImgFilePath];
    for (NSInteger i = 0; i < disImgs.count; i++) {
        NSString *disImpath = [disImgDic stringByAppendingPathComponent: [NSString stringWithFormat:@"draftDis_%02ld",(long)i]];
        ImageMode *imgMode = disImgs[i];
        NSData *imgData = UIImagePNGRepresentation(imgMode.localImg);
        [imgData writeToFile:disImpath atomically:YES];
        [disArrs addObject:disImpath];
    }
    return disArrs;
}

- (NoteDraftMode *)getDraftModeFromCache {
    return self.draftMode;
}

- (void)cleanDraftMode {
    //删除本地的缓存文件
    NSString *uplImgsDic = [FilePathMode getNoteDraftUploadImgFilePath];

    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:uplImgsDic];
    for (NSString *fileName in enumerator) {
        [[NSFileManager defaultManager] removeItemAtPath:[uplImgsDic stringByAppendingPathComponent:fileName] error:nil];
    }
    
    
    NSString *disImgDic = [FilePathMode getNoteDraftDisplayImgFilePath];
    enumerator = [[NSFileManager defaultManager] enumeratorAtPath:disImgDic];
    for (NSString *fileName in enumerator) {
        [[NSFileManager defaultManager] removeItemAtPath:[disImgDic stringByAppendingPathComponent:fileName] error:nil];
    }
    
    self.draftMode = nil;

}




@end
